package com.ctrip.cap.domain.benchmark.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BenchmarkUtil {
	public static int compareVersion(String s1, String s2){
		if( s1 == null && s2 == null )
			return 0;
		else if( s1 == null )
			return -1;
		else if( s2 == null )
			return 1;

		String[]arr1 = s1.split("[^a-zA-Z0-9]+"),
				arr2 = s2.split("[^a-zA-Z0-9]+");

		int i1, i2, i3;

		for(int ii = 0, max = Math.min(arr1.length, arr2.length); 
				ii <= max; ii++){
			if( ii == arr1.length )
				return ii == arr2.length ? 0 : -1;
			else if( ii == arr2.length )
				return 1;

			try{
				i1 = Integer.parseInt(arr1[ii]);
			}
			catch (Exception x){
				i1 = Integer.MAX_VALUE;
			}

			try{
				i2 = Integer.parseInt(arr2[ii]);
			}
			catch (Exception x){
				i2 = Integer.MAX_VALUE;
			}

			if( i1 != i2 ){
				return i1 - i2;
			}

			i3 = arr1[ii].compareTo(arr2[ii]);

			if( i3 != 0 )
				return i3;
		}

		return 0;
	}
	
	public static String getPackageNickName(String packageName){
		// Ctrip_Wireless_View_V6.1_SIT4_TEST.apk
		String nickname = packageName.toUpperCase();
		try {
			if (nickname.contains("Ctrip") || nickname.contains("ctrip")
					|| nickname.contains("CTRIP")) {

				int index = nickname.lastIndexOf("_V");
				nickname = nickname.substring(index+2);
				index = nickname.lastIndexOf(".");
				nickname = nickname.substring(0, index);
				index = nickname.lastIndexOf("_");
				nickname = nickname.substring(0, index + 2);
				nickname = nickname.replace("_", "").replace("SIT", "S");
			} else {
				int index = nickname.indexOf(".");
				nickname = nickname.substring(0, index);
				index = nickname.indexOf("_");
				if (index > -1) {
					nickname = nickname.substring(0, index);
				}
			}
		} catch (Exception e) {
			nickname = packageName;
		}
		
		return nickname;
	}
	
	public static String formatDateAsString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
		return sdf.format(date);
	}


}
